const { DataTypes } = require("sequelize");
const { sequelize } = require("../dbConnect");

// 游戏内容表
const GameContent = sequelize.define('game_contents', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "游戏内容ID"
  },
  image: {
    type: DataTypes.STRING(500),
    allowNull: false,
    comment: "图片URL"
  },
  pinyin: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: "拼音"
  },
  standard_text: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: "标准文本"
  },
  level: {
    type: DataTypes.ENUM('easy', 'medium', 'hard'),
    defaultValue: 'easy',
    comment: "难度级别"
  },
  category: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: "内容分类"
  },
  audio_url: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: "标准音频URL"
  },
  tags: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "标签"
  },
  usage_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "使用次数"
  },
  is_active: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
    comment: "是否启用"
  }
}, {
  tableName: 'game_contents'
});

// 游戏记录表
const GameRecord = sequelize.define('game_records', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "游戏记录ID"
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "用户ID"
  },
  content_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "游戏内容ID"
  },
  user_pronunciation: {
    type: DataTypes.STRING(200),
    allowNull: true,
    comment: "用户发音"
  },
  standard_pronunciation: {
    type: DataTypes.STRING(200),
    allowNull: true,
    comment: "标准发音"
  },
  similarity: {
    type: DataTypes.DECIMAL(5, 2),
    defaultValue: 0.00,
    comment: "相似度"
  },
  score: {
    type: DataTypes.DECIMAL(5, 2),
    defaultValue: 0.00,
    comment: "得分"
  },
  level: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: "评级"
  },
  suggestions: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "建议"
  },
  audio_url: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: "录音URL"
  },
  duration: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: "录音时长(秒)"
  }
}, {
  tableName: 'game_records',
  indexes: [
    {
      fields: ['user_id']
    },
    {
      fields: ['content_id']
    }
  ]
});

module.exports = {
  GameContent,
  GameRecord
};
